﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Core;
using Aribaccio.Device;
using BDPAutodetect.Device;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BDPAutodetect.Simcore
{
    public class DeviceSimulatorCustomer : DeviceSimulatorBase, IDeviceTrans<IDeviceData<IDevice<IDevicePara>>>
    {
        protected Random random = new Random();
        /// <summary>
        /// 接收设备数据委托事件
        /// </summary>
        protected Action<AsciiSlaveData> DealWithAction;
        /// <summary>
        /// 设备通讯实例
        /// </summary>
        protected DeviceSerialAscii<IDevicePara>? DeviceItem { get; set; }
        /// <summary>
        /// 设备仿真数据
        /// </summary>
        protected DeviceSimulatorDataCustomer DeviceSimulatorData { get; set; }
        /// <summary>
        /// 是否打开
        /// </summary>
        public override bool IsOpen => this.DeviceItem?.IsOpen ?? false;
        /// <summary>
        /// 设备地址端口
        /// </summary>
        public override string DeviceAddressPort => this.DeviceItem?.AddressPort ?? string.Empty;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argDeviceName">设备名称</param>
        public DeviceSimulatorCustomer(DeviceModule argModuleType, DeviceSerialAscii<IDevicePara> argDevice)
            : base($"客户端工位{argModuleType.ToInt()}设备仿真", DeviceType.Customer, argModuleType)
        {
            this.DeviceItem = argDevice;
            this.DeviceSimulatorData = new DeviceSimulatorDataCustomer();
            this.DealWithAction += DeviceDataDealWith;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            if (this.DeviceItem != null)
            {
                this.DeviceItem.BindingDevice(this);
            }
        }

        /// <summary>
        /// 接收设备数据[接口实现]
        /// </summary>
        /// <param name="argDeviceData">设备数据</param>
        public Task DeviceDataArrive(IDeviceData<IDevice<IDevicePara>> argDeviceData)
        {
            return Task.Run(() =>
            {
                DeviceDataAcsii<IDevice<IDevicePara>> deviceDataAcsii = (DeviceDataAcsii<IDevice<IDevicePara>>)argDeviceData;
                if (deviceDataAcsii == null || deviceDataAcsii.DataAscii == null) return;

                this.SendMessageIn(deviceDataAcsii.DataAscii.Data);
                this.DealWithAction.Invoke(deviceDataAcsii.DataAscii);
            });
        }

        /// <summary>
        /// 接收设备数据处理
        /// </summary>
        /// <param name="argAsciiData"></param>
        public void DeviceDataDealWith(AsciiSlaveData argAsciiData)
        {
            var commandData = this.DeviceDataParse(argAsciiData);
            if (commandData == null) return;

            //通知UI当前数据
            this.OnActiveDeviceCommandData(commandData);
            //检查定义是否返回，并返回仿真数据
            this.DeviceDataResponseWrite(commandData);
        }

        /// <summary>
        /// 接收设备数据解析
        /// </summary>
        /// <param name="argAsciiData"></param>
        /// <returns></returns>
        protected DeviceCommandData? DeviceDataParse(AsciiSlaveData argAsciiData)
        {
            var commandData = this.GetActiveDeviceCommandData(argAsciiData.Data);
            if (commandData == null) return default;

            commandData.RealResponseCommand = string.Empty;
            commandData.RealCommandContent = argAsciiData.Data;
            commandData.RealCommandReceivedTime = DateTime.Now;
            switch (commandData.DeviceTargetType)
            {
                case DeviceTargetType.Customer_DebugModeStart:
                    this.DeviceSimulatorData.DebugMode = 1;
                    break;
                case DeviceTargetType.Customer_DebugModeFinish:
                    this.DeviceSimulatorData.DebugMode = 0;
                    break;
            }
            return commandData;
        }

        /// <summary>
        /// 设备数据响应写回
        /// </summary>
        /// <param name="argCommandData"></param>
        protected void DeviceDataResponseWrite(DeviceCommandData argCommandData)
        {
            if (!argCommandData.ResponseStatus) return;
            string strResponseCommand = argCommandData.ResponseCommand;
            switch (argCommandData.DeviceTargetType)
            {
                case DeviceTargetType.Customer_DebugModeCheck:
                    strResponseCommand += this.DeviceSimulatorData.DebugMode;
                    break;
                case DeviceTargetType.Customer_SoftwareVersion:
                    //strResponseCommand += DateTime.Now.ToString("yyyyMM");
                    strResponseCommand += "202504";
                    break;
                case DeviceTargetType.Customer_NTCTemperature:
                    int nNTCValue = random.Next(18 * 10, 32 * 10);
                    strResponseCommand += nNTCValue.ToString();
                    break;
                case DeviceTargetType.Customer_EnvironmentTemperature:
                    int nETValue = random.Next(18, 32);
                    strResponseCommand += nETValue.ToString();
                    break;
            }
            argCommandData.RealResponseCommand = strResponseCommand;
            //发送到串口
            this.DeviceItem?.SendSerialPortData(strResponseCommand, false);
            //发送数据到UI界面显示
            this.SendMessageOut(strResponseCommand);
        }
    }

    /// <summary>
    /// 仿真数据
    /// </summary>
    public class DeviceSimulatorDataCustomer
    {
        public int DebugMode { get; set; }
    }
}
